//---------------------------------------------------------------------------
/**
 * \class CBufferedStream
 * \brief This is a base stream to provide buffered stream
 * \version 0.1
 *
 * \author Woo-Hyun Kim (woorung@nhncorp.com, woorung@gmail.com)
 * \author http://blog.naver.com/wisereign .
 *
 * \date 2007/12/05
 */
//---------------------------------------------------------------------------
// CBufferedStream Class
// Author : Woo-Hyun Kim
// Email : woorung@nhncorp.com
// Date : Dec. 5, 2007
//---------------------------------------------------------------------------
#ifndef CBufferedStreamH
#define CBufferedStreamH
//---------------------------------------------------------------------------
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdexcept>
#include <errno.h>

#include "stream_common.h"
//---------------------------------------------------------------------------
using namespace std;
//---------------------------------------------------------------------------
class CBufferedStream : public streambuf {
private :
	virtual streambuf::pos_type seekoff(streambuf::off_type, ios::seekdir dir, ios::openmode);
	virtual streambuf::pos_type seekpos(streambuf::pos_type, ios::openmode);

protected :
	char buffer_[MAX_STREAM_BUF_SIZE];
	int rbuf_size_;

	int fd_;
	uint64_t fsize_;
	uint64_t pos_;

	// copy constructor
	CBufferedStream(const CBufferedStream &rhs);
	// assign constructor
	CBufferedStream& operator=(const CBufferedStream &rhs);

	virtual int underflow();

public :
	CBufferedStream(const char *filename);
	virtual ~CBufferedStream();

	virtual uint64_t seekg(uint64_t pos);
	virtual uint64_t tellg() ;
	virtual uint64_t size() const;

	virtual void resize(int buf_size);
};
//---------------------------------------------------------------------------
#endif
